/*
 *  Document   : common
 *  Created on : 2011-12-11, 14:37:38
 *  Author     : GodSon
 *  Email      : wmails@126.com
 *  Link       : www.btboys.com
 *
 */

var F = {}; //全局变量

(function (f, $) {
    var loadedCss = Array(),
    loadedJs = Array();
	
    f.ns = function (ns, callback) {
        var nsArray = ns.split('.');
        var sEval = "";
        var sNS = "";
        for (var i = 0; i < nsArray.length; i++) {
            if (i != 0)
                sNS += ".";
            sNS += nsArray[i];
			
            sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = new Object();"
        }
        if (sEval != "") {
            sEval = eval(sEval);
			
            if (callback && typeof callback === 'function')
                callback.call(sEval);
        }
    };
	
    f.root = function () {
        var curWwwPath = window.document.location.href;
        var pathName = window.document.location.pathname;
        var pos = curWwwPath.indexOf(pathName);
        var localhostPaht = curWwwPath.substring(0, pos);
        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        return (localhostPaht + projectName + "/");
    };
	
    f.json2str = function (json) {
        var arr = [];
        var fmt = function (s) {
            if (typeof s == 'object' && s != null)
                return f.json2str(s);
            return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
        }
        for (var i in json)
            arr.push("'" + i + "':" + fmt(json[i]));
        return '{' + arr.join(',') + '}';
    };
	
    f.random = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
	
    f.UUID = function () {
        return "UUID-" + (f.random() + f.random() + "-" + f.random() + "-" +
            f.random() + "-" + f.random() + "-" + f.random() + f.random() + f.random());
    };
	
    f.loadJs = function (url, callback) {
        if (loadedJs.inArray(url) != -1) {
            var done = false;
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.language = 'javascript';
            script.charset = 'UTF-8';
            script.src = f.root + url;
            script.onload = script.onreadystatechange = function () {
                if (!done && (!script.readyState || script.readyState == 'loaded' || script.readyState == 'complete')) {
                    done = true;
                    script.onload = script.onreadystatechange = null;
                    if (callback) {
                        callback.call();
                    }
                }
            }
            document.getElementsByTagName("head")[0].appendChild(script);
            loadedJs.push(url);
        }
    };
	
    /**
	 * 加载css
	 * 使用: F.loadCss("style/css.css");
	 **/
    f.loadCss = function (url) {
        if (loadedCss.inArray(url) != -1) {
            var link = document.createElement('link');
            link.rel = 'stylesheet';
            link.type = 'text/css';
            link.media = 'screen';
            link.href = f.root + url;
            document.getElementsByTagName('head')[0].appendChild(link);
            loadedCss.push(url);
        }
    };
	
    /**
	 *扩展Date类型的时间格式化
	 *为datagrid时间格式化准备
	 */
    Date.prototype.format = function (format) {
        /*
		 * eg:format="yyyy-MM-dd hh:mm:ss";
		 */
        if (!format) {
            format = "yyyy-MM-dd hh:mm:ss";
        }
		
        var o = {
            "M+" : this.getMonth() + 1, // month
            "d+" : this.getDate(), // day
            "h+" : this.getHours(), // hour
            "m+" : this.getMinutes(), // minute
            "s+" : this.getSeconds(), // second
            "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
            "S" : this.getMilliseconds()
        // millisecond
        };
		
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
		
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    };
	
    Array.prototype.inArray = function (elem, i) {
        var array = this;
        var len;
        if (indexOf) {
            return indexOf.call(array, elem, i);
        }
        len = array.length;
        i = i ? i < 0 ? Math.max(0, len + i) : i : 0;
		
        for (; i < len; i++) {
            if (i in array && array[i] === elem) {
                return i;
            }
        }
        return -1;
    };
	
    //IE6背景图片缓存
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch (e) {}
	
    if (!!(/(msie)\s*(7(\.\d+)?)/i).exec(navigator.userAgent)) {
        if ($ && $.messager) {
            $(function () {
                $.messager.show({
                    title : "温馨提示",
                    msg : "为了达到最佳的使用效果，建议您升级您的浏览器！",
                    showType : "fade"
                });
            });
        } else {
            window.onLoad = function () {
                alert("为了达到最佳的使用效果，建议您升级您的浏览器！");
            }
        }
    }
    
     __ROOT__ = f.root();
})(F, jQuery);

(function ($) {
    $.ajaxSetup({
        statusCode : {
            404 : function () { //没找到界面
				
            },
            500 : function () { //内部错误
				
            },
            401 : function () { //未授权
				
            },
            408 : function () { //请求时发生超时
				
            },
            503 : function () { //服务已经停止
				
            }
        }
    });
	
})(jQuery);
